package com.transsnet.mybatis.base;

import org.hashids.Hashids;

import java.util.List;
import java.util.stream.Collectors;

public class ObfuscatedId {

    private static final String SALT = "efwlj;etre*jdsl#~lsdf;m";
    private static final Hashids HASHIDS = new Hashids(SALT, 5);

    public static String getObfuscatedId(long id) {
        return HASHIDS.encode(id);
    }

    public static List<String> getObfuscatedIds(List<Long> ids) {
        return ids.stream()
                .map(ObfuscatedId::getObfuscatedId)
                .collect(Collectors.toList());
    }

    public static long getId(String obfuscatedId) {
        long[] decodes = HASHIDS.decode(obfuscatedId);
        if (decodes.length == 1) {
            return decodes[0];
        }
        throw new RuntimeException("unexpected obfuscated id=" + obfuscatedId);
    }

    public static List<Long> getIds(List<String> obfuscatedIds) {
        return obfuscatedIds.stream()
                .map(ObfuscatedId::getId)
                .collect(Collectors.toList());
    }

    public static void main(String[] args) {
        System.out.println(ObfuscatedId.getId("2KpZl"));
        System.out.println(ObfuscatedId.getObfuscatedId(157));
    }

}
